package com.dyw.ycgovfinance.http;

import com.google.gson.JsonObject;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.Body;
import retrofit2.http.POST;
import rx.Observable;

/**
 * Created by Administrator on 2017/6/20.
 */

public interface HttpService {

    //获取预算完成情况数据
    @POST("terminalApp/searchReport")
    Observable<JsonObject> searchReport(@Body RequestBody requestBody);

    //获取最新预算完成情况数据
    @POST("terminalApp/searchNewestData")
    Observable<JsonObject> searchNewestData(@Body RequestBody requestBody);

    /**
     * 获取新闻类型
     * @param requestBody
     * @return
     */
    @POST("terminalApp/serachLmcodeList")
    Observable<JsonObject> getNewsKind(@Body RequestBody requestBody);

    /**
     * 获取新闻列表
     * @param requestBody
     * @return
     */
    @POST("terminalApp/searchNewsList")
    Observable<JsonObject> getNewsList(@Body RequestBody requestBody);

    /**
     * 获取新闻详情
     * @param requestBody
     * @return
     */
    @POST("terminalApp/newsDetail")
    Observable<JsonObject> getNewsDetail(@Body RequestBody requestBody);

    /**
     * 获取公开的新闻类型
     * @param requestBody
     * @return
     */
    @POST("terminalApp/serachPubLmcode")
    Observable<JsonObject> getOpenNewsKind(@Body RequestBody requestBody);

    class Factory {

        private static OkHttpClient generateOkHttpClient() {
            OkHttpClient httpClient = new OkHttpClient.Builder()
                    .readTimeout(5, TimeUnit.MINUTES)
                    .connectTimeout(5, TimeUnit.MINUTES)
                    .writeTimeout(5, TimeUnit.MINUTES)
                    .build();


            return httpClient;
        }

        public static HttpService create(String baseUrl) {

            /*Gson gson = new GsonBuilder()
                    .setLenient()
                    .create();*/

            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(baseUrl + "/")
                    .client(generateOkHttpClient())
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .build();
            return retrofit.create(HttpService.class);
        }
    }
}
